<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px">
            <el-row>
              <el-col :span="8">
                <el-form-item label="关键字：" class="width100">
                  <el-input
                    v-model="tableFrom.keys"
                    placeholder="姓名/ID"
                    size="small"
                    clearable
                  >
                  </el-input></el-form-item
              ></el-col>
              <el-col :span="8">
                <el-form-item label="月份：" class="width100">
                  <el-date-picker
                    v-model="tableFrom.curDate"
                    type="month"
                    value-format="yyyy-MM"
                    placeholder="选择月"
                    size="small"
                    clearable
                    style="width:100%"
                  >
                  </el-date-picker></el-form-item
              ></el-col>
            </el-row>
          </el-form>
        </div>
        <el-button type="primary" size="small" @click="seachList"
          >查询</el-button
        >
        <el-button size="small" @click="exports">导出</el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
        class="table"
        highlight-current-row
      >
        <el-table-column prop="curDate" label="月份" min-width="170" />
        <el-table-column prop="userId" label="用户ID" min-width="130" />
        <el-table-column prop="userName" label="姓名" min-width="130" />
        <el-table-column prop="markName" label="身份" min-width="130" />
        <el-table-column prop="shopName" label="归属店铺" min-width="130" />
        <el-table-column prop="buyFlag" label="本月是否复购" min-width="130">
          <template slot-scope="scope">
            <span v-if="scope.row.buyFlag===0" style="color: red">否</span>
            <span v-if="scope.row.buyFlag===1" style="color: green">是</span>
          </template>
        </el-table-column>
        <el-table-column prop="inviteReward" label="当月推荐金额" min-width="80"/>
        <el-table-column prop="againReward" label="当月复购金额" min-width="80"/>
        <el-table-column prop="totalReward" label="当月奖励金额" min-width="80"/>
        <el-table-column label="操作" min-width="80" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="onOpenDetail(scope.row)"
              class="mr10"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>

    <!--记录-->
    <el-dialog title="奖励详情" :visible.sync="dialogVisibleJI" width="700px">
      <div>
        <el-form label-position="left" inline>
          <el-row>
            <el-col :span="8">
              <el-form-item label="姓名：" class="form-descriptions">
                <span>{{ tableDataLog.detail.userName }}</span>
              </el-form-item></el-col
            >
            <el-col :span="8">
              <el-form-item label="身份：" class="form-descriptions">
                <span>{{ tableDataLog.detail.markName }}</span>
              </el-form-item></el-col
            >
            <el-col :span="8">
              <el-form-item label="归属店铺：" class="form-descriptions">
                <span>{{ tableDataLog.detail.shopName }}</span>
              </el-form-item></el-col
            >
            <el-col :span="6">
              <el-form-item label="月份：" class="form-descriptions">
                <span>{{ tableDataLog.detail.curDate }}</span>
              </el-form-item></el-col
            >
            <el-col :span="6">
              <el-form-item label="当月奖励金额：" class="form-descriptions">
                <span>{{ tableDataLog.detail.totalReward }}</span>
              </el-form-item></el-col
            >
          </el-row>
        </el-form>
      </div>
      <el-table
        v-loading="LogLoading"
        border
        :data="tableDataLog.data"
        style="width: 100%"
      >
        <el-table-column prop="createTime" label="获得时间" />
        <el-table-column prop="reward" label="奖励金额" />
        <el-table-column label="备注" min-width="120">
          <template slot-scope="scope">
            <p>{{ scope.row.buyProduct }}</p>
            <p>
              订单号<el-button
                type="text"
                @click="onOrderDetails(scope.row.orderId)"
                >{{ scope.row.orderNo }}</el-button
              >
            </p>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="tableFromLog.limit"
          :current-page="tableFromLog.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableDataLog.total"
          @size-change="handleSizeChangeLog"
          @current-change="pageChangeLog"
        />
      </div>
    </el-dialog>

    <!--详情-->
    <details-from ref="orderDetail" :orderId="orderId" />
  </div>
</template>

<script>
import {
  rewardMonthListApi,
  rewardInviteListApi,
  getDownloadApi
} from "@/api/financial";
import { shopListApi } from "@/api/user";
import cardsData from "@/components/cards/index";
import zbParser from "@/components/FormGenerator/components/parser/ZBParser";
import detailsFrom from "./orderDetail";
import orderSend from "./orderSend";
import { storeStaffListApi } from "@/api/storePoint";
import Cookies from "js-cookie";
import { isWriteOff } from "@/utils";
import * as categoryApi from "@/api/categoryApi";

export default {
  name: "orderlistDetails",
  components: {
    cardsData,
    zbParser,
    detailsFrom,
    orderSend
  },
  data() {
    return {
      RefuseVisible: false,
      RefuseData: {},
      orderId: 0,
      refundVisible: false,
      refundData: {},
      dialogVisibleJI: false,
      tableDataLog: {
        detail: {},
        data: [],
        total: 0
      },
      tableFromLog: {
        page: 1,
        limit: 10,
        oid: null
      },
      LogLoading: false,
      isCreate: 1,
      editData: null,
      dialogVisible: false,
      tableData: {
        data: [],
        total: 0
      },
      listLoading: true,
      tableFrom: {
        keys: "",
        curDate: "",
        page: 1,
        limit: 20
      },
      orderChartType: {},
      timeVal: [],
      fromList: this.$constants.fromList,
      selectionList: [],
      ids: "",
      orderids: "",
      cardLists: [],
      shopLists: [],
      isWriteOff: isWriteOff()
    };
  },
  mounted() {
    this.getList();
    this.curDate();
  },
  methods: {
    curDate() {
      const nowDate = new Date();
      const date = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() + 1
      };
      const newmonth = date.month > 10 ? date.month : "0" + date.month;
      this.tableFrom.curDate = date.year + "-" + newmonth;
    },
    resetFormRefundhandler() {
      this.refundVisible = false;
    },
    resetFormRefusehand() {
      this.RefuseVisible = false;
    },
    resetForm(formValue) {
      this.dialogVisible = false;
    },
    // 核销订单
    onWriteOff(row) {
      this.$prompt("订单核销", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputErrorMessage: "请输入核销码",
        inputType: "text",
        inputValue: "",
        inputPlaceholder: "请输入核销码",
        inputValidator: value => {
          if (!value) return "核销码不能为空";
        }
      })
        .then(({ value }) => {
          if (value != row.verifyCode) {
            this.$message.info("核销码输入错误");
            return;
          }
          writeUpdateApi(row.verifyCode).then(() => {
            this.$message.success("核销成功");
            this.getList();
          });
        })
        .catch(() => {
          this.$message.info("取消输入");
        });
    },
    seachList() {
      this.tableFrom.page = 1;
      this.getList();
    },
    exports() {
      getDownloadApi(this.tableFrom);
    },
    // 拒绝退款
    RefusehandleClose() {
      this.RefuseVisible = false;
    },
    onOrderRefuse(row) {
      this.orderids = row.id;
      this.RefuseData = {
        orderId: row.orderId,
        reason: ""
      };
      this.RefuseVisible = true;
    },
    RefusehandlerSubmit(formValue) {
      orderRefuseApi({ id: this.orderids, reason: formValue.reason }).then(
        data => {
          this.$message.success("操作成功");
          this.RefuseVisible = false;
          this.getList();
        }
      );
    },
    // 立即退款
    refundhandleClose() {
      this.refundVisible = false;
    },
    onOrderRefund(row) {
      this.refundData = {
        orderId: row.orderId,
        amount: row.payPrice,
        type: ""
      };
      this.orderids = row.id;
      this.refundVisible = true;
    },
    refundhandlerSubmit(formValue) {
      orderRefundApi({
        amount: formValue.amount,
        orderId: this.orderids,
        type: formValue.type
      }).then(data => {
        this.$message.success("操作成功");
        this.refundVisible = false;
        this.getList();
      });
    },
    // 发送
    sendOrder(row) {
      this.$refs.send.modals = true;
      this.$refs.send.getList();
      this.$refs.send.sheetInfo();
      this.orderId = row.id;
    },
    // 订单删除
    handleDelete(row, idx) {
      if (row.isDel) {
        this.$modalSure().then(() => {
          orderDeleteApi({ id: row.id }).then(() => {
            this.$message.success("删除成功");
            this.tableData.data.splice(idx, 1);
          });
        });
      } else {
        this.$confirm(
          "您选择的的订单存在用户未删除的订单，无法删除用户未删除的订单！",
          "提示",
          {
            confirmButtonText: "确定",
            type: "error"
          }
        );
      }
    },
    // 详情
    onOrderDetails(id) {
      this.orderId = id;
      this.$refs.orderDetail.getDetail(id);
      this.$refs.orderDetail.dialogVisible = true;
    },
    // 记录
    onOpenDetail(detail) {
      this.dialogVisibleJI = true;
      this.LogLoading = true;
      rewardInviteListApi({ monthId: detail.id })
        .then(res => {
          this.tableDataLog.detail = detail;
          this.tableDataLog.data = res.list;
          this.tableDataLog.total = res.total;
          this.LogLoading = false;
        })
        .catch(() => {
          this.LogLoading = false;
        });
    },
    pageChangeLog(page) {
      this.tableFromLog.page = page;
      this.getList();
    },
    handleSizeChangeLog(val) {
      this.tableFromLog.limit = val;
      this.getList();
    },
    handleClose() {
      this.dialogVisible = false;
    },
    // 备注
    onOrderMark(row) {
      this.$prompt("订单备注", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputErrorMessage: "请输入订单备注",
        inputType: "textarea",
        inputValue: row.remark,
        inputPlaceholder: "请输入订单备注",
        inputValidator: value => {
          if (!value) return "输入不能为空";
        }
      })
        .then(({ value }) => {
          orderMarkApi({ mark: value, id: row.id }).then(() => {
            this.$message.success("操作成功");
            this.getList();
          });
        })
        .catch(() => {
          this.$message.info("取消输入");
        });
    },
    handleSelectionChange(val) {
      this.selectionList = val;
      const data = [];
      this.selectionList.map(item => {
        data.push(item.id);
      });
      this.ids = data.join(",");
    },
    // 选择时间
    selectChange(tab) {
      this.timeVal = [];
      this.tableFrom.page = 1;
      this.getList();
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.dateLimit = e ? this.timeVal.join(",") : "";
      this.tableFrom.page = 1;
      this.getList();
    },
    // 编辑
    edit(row) {
      this.dialogVisible = true;
      this.orderId = row.id;
      this.editData = {
        orderId: row.orderId,
        totalPrice: row.totalPrice,
        totalPostage: row.totalPostage,
        payPrice: row.payPrice,
        payPostage: row.payPostage,
        gainIntegral: row.gainIntegral
      };
    },
    // 支付
    payOrder(row) {
      this.$confirm("确定支付订单?").then(() => {
        payOrder({ id: row.id }).then(data => {
          this.$message.success("支付成功");
          this.dialogVisible = false;
          this.getList();
        });
      });
    },
    handlerSubmit(formValue) {
      orderUpdateApi(formValue, { id: this.orderId }).then(data => {
        this.$message.success("编辑数据成功");
        this.dialogVisible = false;
        this.getList();
      });
    },
    // 列表
    getList() {
      this.listLoading = true;
      rewardMonthListApi(this.tableFrom)
        .then(res => {
          this.tableData.data = res.list || [];
          this.tableData.total = res.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    getShopList() {
      shopListApi({ page: 1, limit: 9999, status: 1, isDel: 0 }).then(res => {
        this.shopLists = res;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.demo-table-expand {
  /deep/ label {
    width: 83px !important;
  }
}

.refunding {
  span {
    display: block;
  }
}

.selWidth {
  width: 300px;
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}

.el-icon-arrow-down {
  font-size: 12px;
}

.tabBox_tit {
  font-size: 12px !important;
  /*margin: 0 2px 0 10px;*/
  letter-spacing: 1px;
  /*padding: 5px 0;*/
  box-sizing: border-box;
}

.form-descriptions {
  margin-bottom: 0;
}
</style>
